package logic

import (
	"context"
	modelsni "furni-net/models"

	"furni-net/furnirpc/furnirpc"
	"furni-net/furnirpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ToolDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewToolDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ToolDetailLogic {
	return &ToolDetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ToolDetailLogic) ToolDetail(in *furnirpc.ToolDetailRequest) (*furnirpc.ToolDetailResponse, error) {
	// todo: add your logic here and delete this line
	var toolData modelsni.ToolData
	modelsni.DB.Debug().
		Table("tools").
		Joins("left join classes on tools.class_id=classes.id").
		Joins("left join brands on tools.brand_id=brands.id").
		Select("tools.*",
			"classes.name as ClassName",
			"brands.name as BrandName").
		Where("tools.id=?", in.ToolId).
		First(&toolData)
	return &furnirpc.ToolDetailResponse{
		Name:      toolData.Name,
		Price:     float32(toolData.Price),
		Num:       int64(toolData.Num),
		Img:       toolData.Img,
		Info:      toolData.Info,
		ClassName: toolData.ClassName,
		BrandName: toolData.BrandName,
		ToolId:    int64(toolData.ID),
	}, nil
}
